package com.teach.dto;

import java.util.Calendar;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

/**
 * <pre>
 * -  <b>搜索基本条件</b><br/>
 * - 包含 <br/>
 * sort : 进行排序的列<br/>
 * order: asc/desc<br/>
 * key : 通用模糊匹配关键字<br/>
 * beginDate, endDate:开始日期,结束日期 beginDate 0:0:0.000 - endDate 23:59:59.999 支持格式yyyy-MM-dd
 * beginTime, endTime:开始时间,结束时间 支持格式:yyyy-MM-dd HH:mm 
 * ClassName : sys.dto.Search
 * Author : J.L.Zhou
 * Tel : 151 1104 7708
 * E-Mail : 2233875735@qq.com
 * Date : 2020-9-28 16:50:01
 * Version : 1.0
 * Copyright 2020 jlzhou.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 *
 */
public class Search {

	/**
	 * 需要排序的列的名称
	 */
	private String sort;
	/**
	 * 使用asc或者desc排序
	 */
	private String order;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date beginDate, endDate;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
	private Date beginTime, endTime;

	/**
	 * key 通用模糊匹配
	 */
	private String key;


	public String getSort() {
		return sort;
	}

	/**
	 * 进行排序的列
	 * @param sort
	 */
	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	/**
	 * asc/desc
	 * @param order
	 */
	public void setOrder(String order) {
		this.order = order;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	/**
	 * 格式化结束日期，结束时间为当天23：59：59
	 * @param date
	 * @return
	 */
	public Date formatEndDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, 1);
		c.add(Calendar.MILLISECOND, -1);
		return c.getTime();
	}
	

	public Date getBeginDate() {
		if (beginDate == null) {
			return null;
		}
		if (endDate != null) {
			if (beginDate.after(endDate)) {
				return endDate;
			} else {
				return beginDate;
			}
		} else {
			return beginDate;
		}
	}

	@DateTimeFormat(pattern="yyyy-MM-dd")
	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		if (endDate == null) {
			return null;
		}
		if (beginDate != null) {
			if (beginDate.before(endDate)) {
				return formatEndDate(endDate);
			} else {
				return formatEndDate(beginDate);
			}
		} else {
			return formatEndDate(endDate);
		}
	}

	@DateTimeFormat(pattern="yyyy-MM-dd")
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	

	public Date getBeginTime() {
		if(endTime!=null&&beginTime!=null&&endTime.before(beginDate)){
			return endTime;
		};
		return beginTime;
	}

	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public Date getEndTime() {
		if(endTime!=null&&beginTime!=null&&beginTime.after(endTime)){
			return beginTime;
		};
		return endTime;
	}

	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	
}
